<template>
    <el-container style="height: 100vh;">
        <!-- 左侧菜单 -->
        <el-aside width="220px" class="menu">
            <el-menu :default-active="$route.path" router background-color="#2c3e50" text-color="#bdc3c7"
                active-text-color="#3498db" class="side-menu">
                <el-menu-item index="/banks">
                    <el-icon>
                        <collection />
                    </el-icon>
                    <span>题库管理</span>
                </el-menu-item>

                <el-menu-item index="/questions">
                    <el-icon>
                        <document />
                    </el-icon>
                    <span>题目管理</span>
                </el-menu-item>

                <el-menu-item index="/papers">
                    <el-icon>
                        <folder />
                    </el-icon>
                    <span>试卷管理</span>
                </el-menu-item>

                <el-menu-item index="/rules">
                    <el-icon>
                        <setting />
                    </el-icon>
                    <span>规则配置</span>
                </el-menu-item>

                <el-menu-item index="/settings">
                    <el-icon>
                        <user />
                    </el-icon>
                    <span>用户管理</span>
                </el-menu-item>
            </el-menu>
            <!-- 退出按钮固定在底部 -->
            <div class="logout-container">
                <el-menu background-color="#2c3e50" text-color="#bdc3c7" active-text-color="#3498db"
                    class="bottom-menu">
                    <el-menu-item @click="logout" index="/login" class="logout-item">
                        <el-icon><switch-button /></el-icon><span>退出登录</span>
                    </el-menu-item>
                </el-menu>
            </div>
        </el-aside>

        <el-main class="content">
            <router-view />
        </el-main>
    </el-container>
</template>

<script setup>
import { ref } from 'vue';
import {
    Collection,
    Document,
    Folder,
    Setting,
    User,
    SwitchButton
} from '@element-plus/icons-vue';

import { useRouter } from 'vue-router';
import api from '@/services/api';

const router = useRouter();

const logout = () => {
    localStorage.removeItem('token'); // 清除 token
    router.push('/login'); // 跳转登录页
};
</script>

<style scoped>
.menu {
    background-color: #2c3e50;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.side-menu {
    border-right: none;
    flex: 1;
}

.el-menu-item {
    height: 56px;
    font-size: 16px;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: start;    /* 左对齐 */
    padding-left: 24% !important;         /* 控制偏移量 */
}

.el-menu-item:hover {
    background-color: #34495e !important;
}

.el-menu-item.is-active {
    background-color: #34495e !important;
    border-left: 4px solid #3498db;
}

.el-icon {
    margin-right: 12px;
    font-size: 18px;
    margin-right: 8px;
    flex-shrink: 0;
    /* 防止图标被压缩 */
}

/* 确保文字不换行 */
.el-menu-item span {
    white-space: nowrap;
}

.logout-item {
    color: #e74c3c !important;
}

.bottom-menu {
    border-right: none;
    width: 100%;
}

.logout-item .el-icon,
.logout-item span {
    color: #e74c3c !important;
}
</style>